home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP08.ZIP / CHAP08 / PATRON / IDROPTGT.CPP < prev    next >
C/C++ Source or Header  |  1993-05-21  |  13KB  |  518 lines

  1. /*
  2.  * IDROPTGT.CPP
  3.  *
  4.  * Implementation of a DropTarget object for Patron
  5.  *
  6.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  7.  *
  8.  * Kraig Brockschmidt, Software Design Engineer
  9.  * Microsoft Systems Developer Relations
  10.  *
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "patron.h"
  17.  
  18.  
  19.  
  20. /*
  21.  * CDropTarget::CDropTarget
  22.  * CDropTarget::~CDropTarget
  23.  *
  24.  * Constructor Parameters:
  25.  *  pDoc            LPCPatronDoc of the window containing us.
  26.  */
  27.  
  28. CDropTarget::CDropTarget(LPCPatronDoc pDoc)
  29.     {
  30.     m_cRef=0;
  31.     m_pDoc=pDoc;
  32.  
  33.     m_pIDataObject=NULL;
  34.     return;
  35.     }
  36.  
  37.  
  38. CDropTarget::~CDropTarget(void)
  39.     {
  40.     return;
  41.     }
  42.  
  43.  
  44.  
  45.  
  46. /*
  47.  * CDropTarget::QueryInterface
  48.  * CDropTarget::AddRef
  49.  * CDropTarget::Release
  50.  *
  51.  * Purpose:
  52.  *  IUnknown members for CDropTarget object.
  53.  */
  54.  
  55. STDMETHODIMP CDropTarget::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  56.     {
  57.     *ppv=NULL;
  58.  
  59.     //Any interface on this object is the object pointer.
  60.     if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IDropTarget))
  61.         *ppv=(LPVOID)this;
  62.  
  63.     /*
  64.      * If we actually assign an interface to ppv we need to AddRef it
  65.      * since we're returning a new pointer.
  66.      */
  67.     if (NULL!=*ppv)
  68.         {
  69.         ((LPUNKNOWN)*ppv)->AddRef();
  70.         return NOERROR;
  71.         }
  72.  
  73.     return ResultFromScode(E_NOINTERFACE);
  74.     }
  75.  
  76.  
  77. STDMETHODIMP_(ULONG) CDropTarget::AddRef(void)
  78.     {
  79.     return ++m_cRef;
  80.     }
  81.  
  82. STDMETHODIMP_(ULONG) CDropTarget::Release(void)
  83.     {
  84.     ULONG           cRefT;
  85.  
  86.     cRefT=--m_cRef;
  87.  
  88.     if (0L==m_cRef)
  89.         delete this;
  90.  
  91.     return cRefT;
  92.     }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. /*
  99.  * CDropTarget::DragEnter
  100.  *
  101.  * Purpose:
  102.  *  Indicates that data in a drag operation has been dragged over our
  103.  *  window that's a potential target.  We are to decide if it's something
  104.  *  we're interested in or not.
  105.  *
  106.  * Parameters:
  107.  *  pIDataSource    LPDATAOBJECT providing the source data.
  108.  *  grfKeyState     DWORD flags indicating states of keys and mouse buttons.
  109.  *  pt              POINTL coordinates in the client space of the document.
  110.  *  pdwEffect       LPDWORD into which we'll place the appropriate effect
  111.  *                  flag for this point.
  112.  *
  113.  * Return Value:
  114.  *  SCODE           NOERROR
  115.  */
  116.  
  117. STDMETHODIMP CDropTarget::DragEnter(LPDATAOBJECT pIDataSource
  118.     , DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect)
  119.     {
  120.     LPCPages        ppg=m_pDoc->m_pPG;
  121.     HWND            hWnd;
  122.     FORMATETC       fe;
  123.     STGMEDIUM       stm;
  124.     UINT            uRet;
  125.  
  126.     m_fFeedback=FALSE;
  127.     m_pIDataObject=NULL;
  128.  
  129.     if (!m_pDoc->FQueryPasteFromData(pIDataSource, &fe, NULL))
  130.         {
  131.         *pdwEffect=DROPEFFECT_NONE;
  132.         return NOERROR;
  133.         }
  134.  
  135.     //Check if this is a valid drop point.
  136.     uRet=ppg->UTestDroppablePoint(&pt);
  137.     ppg->m_uLastTest=uRet;
  138.  
  139.     if (UDROP_NONE==uRet)
  140.         *pdwEffect=DROPEFFECT_NONE;
  141.     else
  142.         {
  143.         //Default is move if we can, in fact drop here.
  144.         *pdwEffect=DROPEFFECT_MOVE;
  145.  
  146.         if (grfKeyState & MK_CONTROL)
  147.             *pdwEffect=DROPEFFECT_COPY;
  148.         }
  149.  
  150.     m_pIDataObject=pIDataSource;
  151.     m_pIDataObject->AddRef();
  152.  
  153.     /*
  154.      * Determine the size of the data, if we can.  The default is
  155.      * a small rectangle since we can't easily tell what size something
  156.      * will be if we're pulling in a metafile or bitmap.  It's not
  157.      * a good idea to render it here with ::GetData just to find that out.
  158.      * We only know the size if it's our own object in which case a
  159.      * ::GetData will be fast.
  160.      */
  161.  
  162.     if (fe.cfFormat==m_pDoc->m_cf)
  163.         {
  164.         if (SUCCEEDED(pIDataSource->GetData(&fe, &stm)))
  165.             {
  166.             LPPATRONOBJECT  ppo;
  167.             RECT            rc;
  168.  
  169.             ppo=(LPPATRONOBJECT)GlobalLock(stm.hGlobal);
  170.  
  171.             SetRect(&rc, (int)ppo->szl.cx, -(int)ppo->szl.cy, 0, 0);
  172.             RectConvertMappings(&rc, NULL, TRUE);
  173.             SETSIZEL(m_szl, rc.left, rc.top);
  174.  
  175.             m_ptPick=ppo->ptlPick;
  176.             m_fe=ppo->fe;
  177.  
  178.             GlobalUnlock(stm.hGlobal);
  179.             ReleaseStgMedium(&stm);
  180.             }
  181.         }
  182.     else
  183.         {
  184.         SETSIZEL(m_szl, 30, 30);
  185.         m_ptPick.x=0;
  186.         m_ptPick.y=0;
  187.         m_fe.cfFormat=0;
  188.         }
  189.  
  190.  
  191.     //Bring the document window up front and show what a drop will do.
  192.     hWnd=m_pDoc->Window();
  193.     BringWindowToTop(hWnd);
  194.     UpdateWindow(hWnd);
  195.  
  196.     ppg->m_uVScrollCode=0xFFFF;
  197.     ppg->m_uHScrollCode=0xFFFF;
  198.     m_fPendingRepaint=FALSE;
  199.  
  200.     pt.x-=m_ptPick.x;
  201.     pt.y-=m_ptPick.y;
  202.  
  203.     m_ptLast=pt;
  204.     m_fFeedback=TRUE;
  205.     ppg->DrawDropTargetRect(&pt, &m_szl);
  206.  
  207.     return NOERROR;
  208.     }
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. /*
  216.  * CDropTarget::DragOver
  217.  *
  218.  * Purpose:
  219.  *  Indicates that the mouse was moved inside the window represented
  220.  *  by this drop target.  This happens on every WM_MOUSEMOVE, so this
  221.  *  function should be very efficient.
  222.  *
  223.  * Parameters:
  224.  *  grfKeyState     DWORD providing the current keyboard and mouse states
  225.  *  pt              POINTL where the mouse currently is.
  226.  *  pdwEffect       LPDWORD in which to store the effect flag for this point.
  227.  *
  228.  * Return Value:
  229.  *  SCODE           NOERROR
  230.  */
  231.  
  232. STDMETHODIMP CDropTarget::DragOver(DWORD grfKeyState, POINTL pt
  233.     , LPDWORD pdwEffect)
  234.     {
  235.     LPCPages    ppg=m_pDoc->m_pPG;
  236.     UINT        uRet, uLast;
  237.     UINT        xPos, yPos;
  238.  
  239.     if (NULL==m_pIDataObject)
  240.         return NOERROR;
  241.  
  242.     //Check if this is still a valid point.  uRet is used below as well.
  243.     uRet=ppg->UTestDroppablePoint(&pt);
  244.  
  245.     if (UDROP_NONE==uRet)
  246.         *pdwEffect=DROPEFFECT_NONE;
  247.     else
  248.         {
  249.         //Store these before possibly ORing in DROPEFFECT_SCROLL
  250.         *pdwEffect=DROPEFFECT_MOVE;
  251.  
  252.         if (grfKeyState & MK_CONTROL)
  253.             *pdwEffect=DROPEFFECT_COPY;
  254.         }
  255.  
  256.     //If we haven't moved and we are not scrolling, then we're done.
  257.     if ((pt.x-m_ptPick.x==m_ptLast.x) && (pt.y-m_ptPick.y==m_ptLast.y)
  258.         && !((UDROP_INSETHORZ | UDROP_INSETVERT) & ppg->m_uLastTest))
  259.         {
  260.         return NOERROR;
  261.         }
  262.  
  263.     //Remove the last feedback rectangle.
  264.     if (m_fFeedback)
  265.         ppg->DrawDropTargetRect(&m_ptLast, &m_szl);
  266.  
  267.     uLast=ppg->m_uLastTest;
  268.     ppg->m_uLastTest=uRet;
  269.  
  270.     if (UDROP_NONE==uRet)
  271.         {
  272.         //If we are now an invalid point, better repaint as necessary
  273.         if (m_fPendingRepaint)
  274.             {
  275.             UpdateWindow(ppg->m_hWnd);
  276.             m_fPendingRepaint=FALSE;
  277.             }
  278.  
  279.         ppg->m_uVScrollCode=0xFFFF;
  280.         ppg->m_uHScrollCode=0xFFFF;
  281.         m_fFeedback=FALSE;
  282.         return NOERROR;
  283.         }
  284.  
  285.  
  286.     /*
  287.      * Scrolling is a little tricky:  We get a DragOver pulse even
  288.      * if we didn't move.  First we have to delay scrolling for
  289.      * ppg->m_uScrollDelay clock ticks which we can determine using
  290.      * GetTickCount.  Timers do not work here since we may not be
  291.      * yielding to our message loop.
  292.      *
  293.      * Once we know we are scrolling then we determine if we
  294.      * scroll again or if we reset the scrolling state.
  295.      */
  296.  
  297.     if ((UDROP_INSETHORZ & uLast) && !(UDROP_INSETHORZ & uRet))
  298.         ppg->m_uHScrollCode=0xFFFF;
  299.  
  300.     if (!(UDROP_INSETHORZ & uLast) && (UDROP_INSETHORZ & uRet))
  301.         {
  302.         ppg->m_dwTimeLast=GetTickCount();
  303.         ppg->m_uHScrollCode=(0!=(UDROP_INSETLEFT & uRet))
  304.             ? SB_LINELEFT : SB_LINERIGHT;   //Same as UP & DOWN codes.
  305.         }
  306.  
  307.     if ((UDROP_INSETVERT & uLast) && !(UDROP_INSETVERT & uRet))
  308.         ppg->m_uVScrollCode=0xFFFF;
  309.  
  310.     if (!(UDROP_INSETVERT & uLast) && (UDROP_INSETVERT & uRet))
  311.         {
  312.         ppg->m_dwTimeLast=GetTickCount();
  313.         ppg->m_uVScrollCode=(0!=(UDROP_INSETTOP & uRet))
  314.             ? SB_LINEUP : SB_LINEDOWN;
  315.         }
  316.  
  317.     if (0xFFFF==ppg->m_uHScrollCode && 0xFFFF==ppg->m_uVScrollCode)
  318.         ppg->m_dwTimeLast=0L;
  319.  
  320.     //Set the scroll effect on any inset hit.
  321.     if ((UDROP_INSETHORZ